/**
 * ParamTest.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.ParamTest
 * xiaocq Create at 2012-4-10 下午9:24:20
 */
package com.xiaocq.iwordkiller;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.xiaocq.iwordkiller.components.SlipSwitch;
import com.xiaocq.iwordkiller.components.SlipSwitch.OnSwitchListener;
import com.xiaocq.iwordkiller.db.PrepareCursorHelper;

/**
 * com.xiaocq.iwordkiller.ParamTest
 * 
 * @author Mystery Create at 2012-4-10 下午9:24:20
 */
public class ParamTest extends Activity implements OnClickListener{
	private static final String TAG = "ParamTest";
	
	private static final int COUNTS_DIALOG = 1;
	private static final int WORD_TYPE_DIALOG = 2;
	private static final int UNIT_DIALOG = 3;
	private static final int COSTUM_INPUT = 4;
	
	private Param mParam;
	private Button mBackToMenu, mLoad,mTest;
	private Button mWordType, mCounts, mUnit;
	private SlipSwitch mRange, mType;
	private LinearLayout mCostumGroup, mUnitGroup;

	private String[] items;
	private int[] counts;
	
	private PrepareCursorHelper helper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.param_test);
		
		mParam = new Param(this);
		mParam.load();
		
		initComponents();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(helper != null) helper.close();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case WORD_TYPE_DIALOG:
			return new AlertDialog.Builder(this).setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mParam.testId = which + 51;
					mParam.testCounts = counts[which];
					mWordType.setText(items[which]);
					if(mParam.testCounts < 30){
						mCounts.setText(mParam.testCounts + "个");
					}
				}
			}).create();
		case COUNTS_DIALOG:
			return new AlertDialog.Builder(this).setItems(R.array.test_counts_values, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					int tmpCounts = mParam.testCounts;
					if(which == 0) tmpCounts = 30;
					if(which == 1) tmpCounts = 50;
					if(which == 2) tmpCounts = 70;
					if(which == 3) showDialog(COSTUM_INPUT);
					mParam.testCounts = mParam.testCounts < tmpCounts ? mParam.testCounts : tmpCounts;
					mCounts.setText(mParam.testCounts + "个");
				}
			}).create();
		case COSTUM_INPUT:
			final LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			final View view = inflater.inflate(R.layout.input_dialog, null);
			final EditText input = (EditText) view.findViewById(R.id.dialog_et_input);
			return new AlertDialog.Builder(this).setView(view)
					.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							Integer counts = Integer.valueOf(input.getText().toString());
							if(counts > 250) counts = 250;
							mParam.testCounts = mParam.testCounts < counts ? mParam.testCounts : counts;
							mCounts.setText(mParam.testCounts + "个");
							input.setText("");
						}
					}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							input.setText("");
						}
					}).create();
		case UNIT_DIALOG:
			helper = new PrepareCursorHelper(this);
			helper.open();
			return new AlertDialog.Builder(this).setItems(R.array.unit_list, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mParam.testId = which + 1;
					mUnit.setText("List " + mParam.testId);
					prepareUnitCounts(mParam.testId);
				}
			}).create();
		default:
			return super.onCreateDialog(id);
		}
	}

	private void initComponents() {
		mBackToMenu = (Button) findViewById(R.id.param_test_backToMenu); mBackToMenu.setTag(R.id.param_test_backToMenu);
		mLoad = (Button) findViewById(R.id.param_test_load); mLoad.setTag(R.id.param_test_load);
		mRange = (SlipSwitch) findViewById(R.id.param_test_switch_range);	mRange.setTag(R.id.param_test_switch_range);
		mType = (SlipSwitch) findViewById(R.id.param_test_switch_type); mType.setTag(R.id.param_test_switch_type);
		mWordType = (Button) findViewById(R.id.param_test_wordType); mWordType.setTag(R.id.param_test_wordType);
		mCounts = (Button) findViewById(R.id.param_test_counts); mCounts.setTag(R.id.param_test_counts);
		mUnit = (Button) findViewById(R.id.param_test_unit); mUnit.setTag(R.id.param_test_unit);
		mTest = (Button) findViewById(R.id.param_test_start); mTest.setTag(R.id.param_test_start);
		mCostumGroup = (LinearLayout) findViewById(R.id.custom_test_group);
		mUnitGroup = (LinearLayout) findViewById(R.id.unit_test_group);
		
		mRange.setSwitchState(false);
		mType.setSwitchState(true);
		mRange.setImageResource(R.drawable.switch_range, R.drawable.btn_slip);
		mType.setImageResource(R.drawable.bg_switch, R.drawable.btn_slip);
		
		mRange.setOnSwitchListener(new OnSwitchListener() {
			public void onSwitched(boolean isSwitchOn) {
				changeInitValue(isSwitchOn);
				switchRange(isSwitchOn);
			}
		});
		
		mType.setOnSwitchListener(new OnSwitchListener() {
			public void onSwitched(boolean isSwitchOn) {
				mParam.testType = !isSwitchOn;
				Log.d(TAG, "what's this value!!" + mParam.testType);
			}
		});
		
		if(mParam.isNewTest == false){
			Toast.makeText(this, R.string.tip_loadOrNot, Toast.LENGTH_LONG).show();
			mParam.initNewParam();
			mLoad.setEnabled(true);
		}else{
			mParam.initNewParam();
			mLoad.setEnabled(false);
		}

		mBackToMenu.setOnClickListener(this);
		mRange.setOnClickListener(this);
		mType.setOnClickListener(this);
		mWordType.setOnClickListener(this);
		mCounts.setOnClickListener(this);
		mUnit.setOnClickListener(this);
		mTest.setOnClickListener(this);
		mLoad.setOnClickListener(this);
		
		prepareDialogItems();
		
	}

	private void switchRange(boolean model) {
		if (model) {
			mWordType.setText(items[0]);
			mCounts.setText("30个");
			mCostumGroup.setVisibility(View.VISIBLE);
			mUnitGroup.setVisibility(View.GONE);
		} else {
			mCostumGroup.setVisibility(View.GONE);
			mUnitGroup.setVisibility(View.VISIBLE);
			mUnit.setText("List 1");
		}
	}

	private void changeInitValue(boolean model) {
		if (model) {
			mParam.testCounts = 30;
			mParam.testId = 51;
		} else {
			mParam.testCounts = 73;
			mParam.testId = 1;
		}
	}
	
	private void prepareDialogItems(){
		String[] uResults = new String[50];
		String[] results = new String[]{
			"所有单词",
			"未测单词",
			"已测单词",
			"答对单词",
			"答错单词"
		};
		counts = new int[5];
		PrepareCursorHelper helper = new PrepareCursorHelper(this);
		
		helper.open();
		
		for(int i=0; i<5; i++){
			counts[i] = helper.getAllCounts(i + 51);
			results[i] = results[i] + "(" + counts[i] + ")"; 
		}
		
		helper.close();
		
		for(int i=0; i<50; i++){
			uResults[i] = "List " + (i+1);
		}
		items = results;
	}
	
	private void prepareUnitCounts(int unitId){
		int counts;
		
		counts = helper.getUnitCounts(unitId, false);
		mParam.testCounts = counts;
	}

	public void onClick(View v) {
		int which = (Integer) v.getTag();
		switch(which){
		case R.id.param_test_backToMenu:
			finish();
			break;
		case R.id.param_test_wordType:
			showDialog(WORD_TYPE_DIALOG);
			break;
		case R.id.param_test_counts:
			showDialog(COUNTS_DIALOG);
			break;
		case R.id.param_test_unit:
			showDialog(UNIT_DIALOG);
			break;
		case R.id.param_test_start:
			check();
			gotoTest();
			break;
		case R.id.param_test_load:
			mParam.load();
			gotoTest();
			break;
		}
	}
	private void check(){
		if(mParam.testCounts <=0){
			Toast.makeText(this, R.string.tip_error, Toast.LENGTH_SHORT).show();
			return;
		}
	}
	
	private void gotoTest(){
		Intent intent = mParam.getIntentOfParam();
		intent.setClass(ParamTest.this, WordTest.class);
		startActivity(intent);
		finish();
	}
}
